package com.example.tests.lesson1.hw1.tests;

import static org.junit.Assert.fail;

import java.util.List;
import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.Select;
import org.testng.annotations.*;


import com.example.tests.lesson1.hw1.GroupModel;

public class TestBase {


	protected GroupModel group;
	public static WebDriver driver;
	public static String baseUrl = "http://localhost/addressbook/";
	public static boolean acceptNextAlert = true;
	private static StringBuffer verificationErrors = new StringBuffer();

	@BeforeTest
	public void setUp() throws Exception {
		driver = new FirefoxDriver();
		driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);


	}

	@AfterSuite
	public void tearDown() throws Exception {
		stop();
	}

	protected GroupModel initGroup() {
		if (group == null) {
			group = new GroupModel();
		}
		return group;
	}
	

	public void submitGroupForm() {
		By locator = By.cssSelector("input[name=\"submit\"]");
		clickOn(locator);
	}

	public void submitContactForm() {
		By locator = By.cssSelector("input[name=\"submit\"]");
		clickOn(locator);
	}

	public void groupPopulateForm(GroupModel group) {
		groupEnterName(group);
		groupEnterHeader(group);
		groupEnterFooter(group);
	}

	public void groupEnterFooter(GroupModel group) {
		By locator = By.cssSelector("textarea[name=\"group_footer\"]");
		fillTextField(locator, group.footer);
	}

	public void groupEnterHeader(GroupModel group) {
		By locator = By.cssSelector("textarea[name=\"group_header\"]");
		fillTextField(locator, group.header);
	}

	public void groupEnterName(GroupModel group) {
		By locator = By.cssSelector("input[name=\"group_name\"]");
		fillTextField(locator, group.name);
	}

	public void openNewGroupFormPage() {
		String locator = "input[name=\"new\"]";
		clickOn(By.cssSelector(locator));
	}

	public void stop() {
		if (driver != null) {
			driver.quit();
			String verificationErrorString = verificationErrors.toString();
			if (!"".equals(verificationErrorString)) {
				fail(verificationErrorString);
			}
		}
	}

	public void returnToGroupList() {
		By locator = By
				.xpath("//div[@class=\"msgbox\"]//a[@href=\"group.php\"]");
		clickOn(locator);
		// driver.findElement(By.linkText("group page")).click();
	}

	public void returnToMainPage() {
		By locator = By.linkText("home page");
		clickOn(locator);
	}

	public void goToMainPage() {
		driver.get(baseUrl + "index.php");
	}

	public List<WebElement> getAllItemsFromSelect(By locator) {
		// options
		List<WebElement> list = driver.findElements(locator);
		return list;
	}

	public void navigateToMainPage() {
		// By locator = By.xpath("//div[@id=\"nav\"]/ul/li[1]/a");
		By locator = By.cssSelector("div[id=\"nav\"] ul li a[href=\"./\"]");
		// By locator = By.linkText("home");
		clickOn(locator);
	}

	public void navigateToNewContact() {
		By locator = By.xpath("//div[@id=\"nav\"]/ul/li[2]/a");
		// By locator =
		// By.cssSelector("div[id=\"nav\"] ul li a[href=\"edit.php\"]");
		// By locator = By.linkText("add new");
		clickOn(locator);
	}

	public void navigateToGroupList() {
		// driver.findElement(By.xpath("//div[@id=\"nav\"]/ul/li[3]/a"));
		// driver.findElement(By.cssSelector("div[id=\"nav\"] ul li a[href=\"group.php\"]"));
		// driver.findElement(By.linkText("groups")).click();
		By locator = By
				.cssSelector("div[id=\"nav\"] ul li a[href=\"group.php\"]");
		clickOn(locator);
	}

	public void selectItemFromDropDown(WebElement dropDown, String value) {
		new Select(dropDown).selectByVisibleText(value);
	}

	public void fillTextField(By locator, String value) {
		clear(locator);
		sendKeys(locator, value);
	}

	public void clickOn(By by) {
		driver.findElement(by).click();
	}

	public void sendKeys(By locator, String value) {
		driver.findElement(locator).sendKeys(value);
	}

	public void clear(By locator) {
		driver.findElement(locator).clear();
	}
}
